//
//  LoginViewController.m
//  JimiPay
//
//  Created by yull on 16/1/6.
//  Copyright (c) 2016年 com.shengyun. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"
#import "AppCenterViewController.h"
#import "UserCenterViewController.h"
#ifdef TAB3_URL
#import "CaiyoudaoViewController.h"
#endif
#if SHOW_WEIZHUAN == 1
#import "WeiZhuanViewController.h"
#endif
#import "ServiceViewController.h"

#import "NSString+MD5.h"
#import "UIAlertView+BlocksKit.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = MainColor;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(endEditing:)];
    [self.view addGestureRecognizer:tapGesture];
    
    self.iconIV.image = [UIImage imageNamed:@"app_icon_60"];
    self.appNameL.text = APPNAME;
    
    NSString *custMobile = [[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE];
    self.accountTF.text = custMobile;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (IBAction)button_login:(id)sender {
    
    [self.view endEditing:YES];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [TDHttpEngine requestForLoginWithCustMobile:self.accountTF.text custPwd:[self.passwordTF.text MD5] complete:^(BOOL succeed, NSString *msg, NSString *cod) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (succeed) {
            [self setupTabBarController];
            [self checkVersion];
        }
        else{
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
}

- (IBAction)button_regist:(id)sender {
    
    RegistViewController *controller = [[RegistViewController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}

- (IBAction)forgetPassword:(id)sender {
    
    RegistViewController *controller = [[RegistViewController alloc] init];
    controller.forgetPassword = YES;
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)setupTabBarController {
    
    AppCenterViewController * appCenterVC = [[AppCenterViewController alloc] init];
    appCenterVC.tabBarItem.title = @"应用中心";
    appCenterVC.tabBarItem.image = [[UIImage imageNamed:@"tab_appcenter"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    appCenterVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"tab_appcenter_s"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UINavigationController *appcenterNav = [[UINavigationController alloc] initWithRootViewController:appCenterVC];
    
    UserCenterViewController * userCenterVC = [[UserCenterViewController alloc] init];
    userCenterVC.tabBarItem.title = [APPNAME isEqualToString:@"财有道"] ? @"认证中心" : @"个人中心";
    userCenterVC.tabBarItem.image = [[UIImage imageNamed:@"tab_usercenter"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    userCenterVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"tab_usercenter_s"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UINavigationController *userCenterNav = [[UINavigationController alloc] initWithRootViewController:userCenterVC];

#ifdef TAB3_URL
    CaiyoudaoViewController *caiyoudaoVC = [[CaiyoudaoViewController alloc] init];
    caiyoudaoVC.tabBarItem.title = APPNAME;
    caiyoudaoVC.tabBarItem.image = [[UIImage imageNamed:@"tab_caiyoudao"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    caiyoudaoVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"tab_caiyoudao_s"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UINavigationController *caiyoudaoNav = [[UINavigationController alloc] initWithRootViewController:caiyoudaoVC];
#endif
#if SHOW_WEIZHUAN == 1
    WeiZhuanViewController * weizhuanVC = [[WeiZhuanViewController alloc] init];
    weizhuanVC.tabBarItem.title = @"微赚";
    weizhuanVC.tabBarItem.image = [[UIImage imageNamed:@"tab_weizhuan"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    weizhuanVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"tab_weizhuan_s"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UINavigationController *weizhuanNav = [[UINavigationController alloc] initWithRootViewController:weizhuanVC];
#endif
    ServiceViewController * serviceVC = [[ServiceViewController alloc] init];
    serviceVC.tabBarItem.title = @"服务台";
    serviceVC.tabBarItem.image = [[UIImage imageNamed:@"tab_service"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    serviceVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"tab_service_s"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UINavigationController *serviceNav = [[UINavigationController alloc] initWithRootViewController:serviceVC];
    
    NSMutableArray *vcArray = [NSMutableArray array];
    [vcArray addObject:appcenterNav];
    [vcArray addObject:userCenterNav];
#ifdef TAB3_URL
    [vcArray addObject:caiyoudaoNav];
#endif
#if SHOW_WEIZHUAN == 1
    [vcArray addObject:weizhuanNav];
#endif
    [vcArray addObject:serviceNav];
    
    UITabBarController *mainTBC = [[UITabBarController alloc] init];
    mainTBC.viewControllers = vcArray;
    
    [mainTBC.viewControllers enumerateObjectsUsingBlock:^(UIViewController *obj, NSUInteger idx, BOOL *stop) {
        [obj.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];
        obj.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -2);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.view.window.rootViewController = mainTBC;
    });
}

- (void)checkVersion
{
    [TDHttpEngine requestFor_CHECKVERSION_WithCustId:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID] custMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE] appVersion:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] appPlatform:@"1" complete:^(BOOL succeed, NSString *msg, id reqBody) {
        if (succeed) {
            NSInteger checkState = [reqBody[@"checkState"] integerValue];
            NSString *fileDesc = reqBody[@"fileDesc"];
            NSString *fileUrl = reqBody[@"fileUrl"];
            UIAlertView *alertView = [UIAlertView bk_alertViewWithTitle:@"有新版本更新" message:fileDesc];
            if (checkState == 1 || checkState == 2) {
                [alertView bk_addButtonWithTitle:@"更新" handler:^{
                    [[UIApplication  sharedApplication] openURL:[NSURL URLWithString:fileUrl]];
                    if (checkState == 2) {
                        exit(0);
                    }
                }];
                
                if (checkState == 1) {
                    [alertView bk_setCancelButtonWithTitle:@"取消" handler:^{
                        
                    }];
                }
                
                [alertView show];
            }
        }
    }];
}

@end
